<template>
  <div class="register-page">
    <div class="register-bg"></div>
    <div class="register-card">
      <h1 class="register-title">用户注册</h1>
      <el-form :model="form" label-width="70px" class="register-form">
        <el-form-item label="用户名">
          <el-input v-model="form.username" placeholder="请输入3-20位用户名"/>
        </el-form-item>
        <el-form-item label="密码">
          <el-input type="password" v-model="form.password" placeholder="请输入6-32位密码"/>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="form.phone" placeholder="请输入手机号"/>
        </el-form-item>
        <el-form-item label="验证码">
          <el-row :gutter="10">
            <el-col :span="14">
              <el-input v-model="form.verify" placeholder="请输入验证码"/>
            </el-col>
            <el-col :span="10">
              <el-button type="info" @click="getCode">获取验证码</el-button>
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="registered">完成注册</el-button>
        </el-form-item>
        <el-form-item>
          <span>已有账号？</span>
          <el-button type="text" @click="$router.push('/Login')">立即登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import axios from "axios";
import {ElMessage} from "element-plus";

export default {
  name: "Registered",
  data() {
    return {
      form: {
        username: "",
        password: "",
        phone: "",
        verify: ""
      }
    };
  },
  methods: {
    async getCode() {
      if (!this.form.phone) {
        ElMessage.warning("请先输入手机号");
        return;
      }
      try {
        let {data: data} = await axios.post('http://localhost:8080/user/verify', this.form);
        if (data.code == 200) {
          console.log(data)
          ElMessage.success(data.msg);
        } else {
          ElMessage.error(data.msg);
        }
      } catch (err) {
        ElMessage.error("网络错误");
      }
    },
    async registered() {
      let {data: data} = await axios.post('http://localhost:8080/user/registered', this.form);
      console.log(data)
      if (data.code == 200) {
        ElMessage.success(data.msg);
        // this.$router.push('/Login');
      } else {
        ElMessage.error(data.msg);
      }
    }
  }
};
</script>

<style scoped>
.register-page {
  width: 100vw;
  height: 100vh;
  position: relative;
}

.register-bg {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(135deg, #43cbff 0%, #9708cc 100%);
}

.register-card {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 400px;
  padding: 30px;
  background: #fff;
  border-radius: 10px;
}

.register-title {
  text-align: center;
  margin-bottom: 20px;
  color: #333;
}

.register-form {
  margin-top: 20px;
}
</style>